<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\CompanyAssessLogFormat;
use App\Format\CompanyFormat;
use App\Http\Requests\CheckRequest;
use App\Http\Requests\CreateCompanyAssessLogRequest;
use App\Http\Requests\CreateCompanyRequest;
use App\Services\CompanyService;
use Illuminate\Http\Request;

class CompanyController extends Controller
{
    protected $companyService;

    public function __construct(CompanyService $companyService)
    {
        $this->companyService = $companyService;
    }

    public function getCompanyAdminList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }
        if ($request->input('check_status')) {
            $search['check_status'] = $request->input('check_status');
        }
        $res = $this->companyService->getAdminList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function getList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }
        $res = $this->companyService->getList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function create(CreateCompanyRequest $createCompanyRequest)
    {
        $format = new CompanyFormat($createCompanyRequest->all());
        $format->setRegisterAdminUserId($createCompanyRequest->input('user_id'));
        $this->companyService->create($format);
        return renderSuccessJson();
    }

    public function update(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少 id');
        }
        $format = new CompanyFormat($request->all());
        $this->companyService->update($format);
        return renderSuccessJson();
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $platform = $request->input('platform');
        if (empty($id)) {
            throw new ApiException('缺少 id');
        }
        $res = $this->companyService->detail($id, $platform);
        return renderSuccessJson($res);
    }

    public function setCompanyForCheck(CreateCompanyRequest $createCompanyRequest)
    {
        $userId = $createCompanyRequest->input('user_id');
        if (! $this->companyService->checkUser($userId)) {
            return renderErrorJson(1, '用户信息未审核');
        }
        $format = new CompanyFormat($createCompanyRequest->all());
        $format->setRegisterUserId($userId);
        $this->companyService->setCompanyForCheck($format);
        return renderSuccessJson();
    }

    public function checkCompany(CheckRequest $checkRequest)
    {
        $id = $checkRequest->input('id');
        $checkStatus = $checkRequest->input('check_status');
        $checkMessage = $checkRequest->input('check_message') ?? '';
        $this->companyService->checkCompany($id, $checkStatus, $checkMessage);
        return renderSuccessJson();
    }

    public function getMyCompany(Request $request)
    {
        $userId = $request->input('user_id');
        $companise = $this->companyService->getMyCompanies($userId);
        return renderSuccessJson($companise);
    }

    public function deleteCompany(Request $request)
    {
        $id = $request->input('id');
        $this->companyService->deleteCompany($id);
        return renderSuccessJson();
    }

    public function assessCompany(CreateCompanyAssessLogRequest $createCompanyAssessLogRequest)
    {
        $params = $createCompanyAssessLogRequest->input('assess');
        foreach ($params as $param) {
            $format = new CompanyAssessLogFormat($param);
            $this->companyService->assessCompany($format);
        }
        return renderSuccessJson();
    }

    public function getCompanyAssessLogList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $userId = $request->input('user_id');
        $search = [];
        if ($request->input('company_name')) {
            $search['company_name'] = $request->input('company_name');
        }
        $res = $this->companyService->companyAssessLogList($page, $pageSize, $userId, $search);
        return renderSuccessJson($res);
    }

    public function getCompanyAssessLogDetail(Request $request)
    {
        $companyId = $request->input('company_id');
        $assessTempId = $request->input('assess_template_id');
        $res = $this->companyService->getCompanyAssessDetail($companyId, $assessTempId);
        return renderSuccessJson($res);
    }

    public function sort(Request $request)
    {
        $id = $request->input('id');
        $sort = $request->input('sort');
        $this->companyService->setCompanySort($id, $sort);
        return renderSuccessJson();
    }
}
